package org.edu.modules.research.rwork.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.research.rbase.entity.VeJkyOrganizeUser;
import org.edu.modules.research.rwork.entity.VeJkyMeetUser;
import org.edu.modules.research.rwork.mapper.VeJkyMeetMapper;
import org.edu.modules.research.rwork.entity.VeJkyMeet;
import org.edu.modules.research.rwork.dto.VeJkyMeetDto;
import org.edu.modules.research.rwork.service.VeJkyMeetService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.research.rwork.service.VeJkyMeetUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 实现类
 */
@Service
public class VeJkyMeetServiceImpl extends BaseServiceImpl<VeJkyMeet, VeJkyMeetMapper> implements VeJkyMeetService {

    @Autowired
    private VeJkyMeetUserService meetUserService;

    public IPage<VeJkyMeetDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeJkyMeetDto findDtoById(String id) {
        VeJkyMeetDto dto =mapper.findDtoById(id);

        LambdaQueryWrapper<VeJkyMeetUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(VeJkyMeetUser::getJkyUserId);
        lambdaQueryWrapper.eq(VeJkyMeetUser::getMeetId,dto.getId());
        List<VeJkyMeetUser> veJkyOrganizeUsers = meetUserService.listByWrapper(lambdaQueryWrapper);
        List<Integer> userIds = new ArrayList<>();
        for (VeJkyMeetUser user: veJkyOrganizeUsers) {
            userIds.add(user.getJkyUserId());
        }
        dto.setUserIds(userIds);
        return dto;
    }

    /**
    * 新增或者修改
    */
    @Transactional
    public void insertOrUpdate(VeJkyMeetDto dto,Boolean isAdd){
        Date now = new Date();

        if(isAdd){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            dto.setCreateUid(sysUser.getId());
            dto.setCreateName(sysUser.getRealname());
            dto.setCreateTime(now);
            super.insert(dto);
        }else{
            meetUserService.deleteByPid(dto.getId()+"");
            super.update(dto);
        }
        List<VeJkyMeetUser> users = new ArrayList<>();
        for (Integer userId:dto.getUserIds()) {
            VeJkyMeetUser user = new VeJkyMeetUser();
            user.setJkyUserId(userId);
            user.setMeetId(dto.getId());
          //  user.setCreateTime(now);
            user.setSignStatus(0);
            users.add(user);
        }
        meetUserService.insert(users);
    }

}
